package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional//service层方法涉及两个及以上的dml操作时，需要添加事务以保持原子性
    public void saveDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        dishMapper.insertDish(dish);//保存菜品
        Long dishId = dish.getId();//通过主键回显获取插入后菜品自增长的id值
        if (dishFlavors != null && dishFlavors.size() != 0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                if (dishFlavor.getDishId() == null) { //风味的菜品id没有设置则需要进行设置
                    dishFlavor.setDishId(dishId);
                }
            }
            dishFlavorMapper.insertDishFlavors(dishFlavors);//保存菜品对应的风味
        }
    }

    @Override
    public PageResult pageSearch(DishPageQueryDTO dishPageQueryDTO) {
        //注意：PageHelper.startPage一定要放在方法的首行！
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> results = dishMapper.page(dishPageQueryDTO);
        PageInfo<DishVO> pageInfo = new PageInfo<>(results);
        log.info("结果总数：{}，查询结果：{}", pageInfo.getTotal(), pageInfo.getList());
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @Transactional
    public void deleteDish(List<String> ids) {
        //查询未关联套餐且处于停售状态的菜品有哪些
        List<Dish> dishList = dishMapper.selectDishForDelete(ids);
        List<Long> idList=new ArrayList<>();
        for (Dish dish : dishList) {//构造用于删除的菜品idList
            idList.add(dish.getId());
        }
        //将这些id对应的数据在菜品表中删除
        dishMapper.deleteDish(idList);
        //将这些id对应的数据在口味表中删除
        dishFlavorMapper.deleteFlavorByDishId(idList);
    }

    @Override
    public DishVO searchDish(Long id) {
        return dishMapper.searchDishById(id);
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {

        //将DishDTO中有关Dish表的信息拷贝到一个Dish对象中，传递给Mapper层，这样比直接传递DTO对象更规范
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        /*
            修改口味数据：
            根据展示的前端产品原型，传来的DishFlavor数组可能包含旧有但被修改的口味，也包含新增的口味，还有些口味被删除，直接update较复杂
            所以直接将dishId对应的口味数据全部删除，然后在插入新的
         */
        //先删除菜品对应的全部风味
        List<Long> dishId=new ArrayList<>();
        dishId.add(dishDTO.getId());
        dishFlavorMapper.deleteFlavorByDishId(dishId);
        //再将对应的修改的风味全部加入，由于dishId可能为空，所以要对dishId进行填充
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if(!dishFlavors.isEmpty()&&dishFlavors!=null) {//传来的菜品数据可能没有口味数据，所以要判空
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dishDTO.getId());
            }
        }
    }

    @Override
    public List<DishVO> searchDishByCategory(Long categoryId) {
        List<DishVO> dishVOList=dishMapper.selectDishVOByCategoryId(categoryId);
        return dishVOList;
    }

    @Override
    public void startOrStop(Long id, Integer status) {
        dishMapper.updateDish(Dish.builder().id(id).status(status).build());
    }

    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.selectDishByCategoryId(categoryId);
    }
}
